package com.aizhixin.lab.project.course.service;

import cn.hutool.core.date.DateUtil;
import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.core.PageUtil;
import com.aizhixin.lab.common.domain.PageDomain;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.repository.UserRepository;
import com.aizhixin.lab.post.entity.Business;
import com.aizhixin.lab.post.repository.BusinessRepository;
import com.aizhixin.lab.post.server.PostSkillService;
import com.aizhixin.lab.project.course.domain.AddTeacherDomain;
import com.aizhixin.lab.project.course.domain.saveBftDomain;
import com.aizhixin.lab.project.course.entity.*;
import com.aizhixin.lab.project.course.repository.*;
import com.aizhixin.lab.project.course.vo.*;
import com.aizhixin.lab.project.template.data.NewProjectData;
import com.aizhixin.lab.project.template.domain.*;
import com.aizhixin.lab.project.template.entity.*;
import com.aizhixin.lab.project.template.repository.*;
import com.aizhixin.lab.project.template.service.ModuleTaskService;
import com.aizhixin.lab.project.template.service.ProjectModuleService;
import com.aizhixin.lab.project.template.service.ProjectService;
import com.aizhixin.lab.project.template.service.WorkOrderService;
import com.aizhixin.lab.task.core.ExerciseType;
import com.aizhixin.lab.task.domain.StuTaskInforDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
@Transactional
public class TeacherProjectService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectClassStuRepository stuRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;
    @Autowired
    private ProjectClassRepository classRepository;
    @Autowired
    private ProjectGroupRepository projectGroupRepository;
    @Autowired
    private LanguageRepository languageRepository;
    //    @Autowired
//    private ProjectRepository projectRepository;
//    @Autowired
//    private ModuleTaskFileRepository moduleTaskFileRepository;
//    @Autowired
//    private ModuleTaskRepository moduleTaskRepository;
//    @Autowired
//    private ProjectCourseRepository projectCourseRepository;
    @Autowired
    private ProjectModuleRepository projectModuleRepository;
    @Autowired
    private ModuleTaskFileRepository taskFileRepository;
    @Autowired
    private TaskWorkOrderRepository orderRepository;
    @Autowired
    private TaskWorkOrderFileRepository orderFileRepository;
    @Autowired
    private KnowledgePointRepository knowledgePointRepository;
    //    @Autowired
//    private ProjectPostRepository ProjectPostRepository;
//    @Autowired
//    private ProjectRoleRepository projectRoleRepository;
//    @Autowired
//    private ProjectSkillPointsRepository projectSkillPointsRepository;
//    @Autowired
//    private ProjectSkillRepository projectSkillRepository;
//    @Autowired
//    private ProjectVideoRepository projectVideoRepository;
    @Autowired
    private ModuleTaskRepository taskRepository;
    @Autowired
    private ProjectModuleService projectModuleService;
    @Autowired
    private ModuleTaskService moduleTaskService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private TeacherModuleTaskFileRepository teacherModuleTaskFileRepository;
    @Autowired
    private TeacherModuleTaskRepository teacherModuleTaskRepository;
    @Autowired
    private TeacherProjectCourseRepository teacherProjectCourseRepository;
    @Autowired
    private TeacherProjectModuleRepository teacherProjectModuleRepository;
    @Autowired
    private TeacherProjectPostRepository teacherProjectPostRepository;
    @Autowired
    private TeacherProjectRepository teacherProjectRepository;
    @Autowired
    private TeacherProjectSkillPointsRepository teacherProjectSkillPointsRepository;
    @Autowired
    private TeacherProjectSkillRepository teacherProjectSkillRepository;
    @Autowired
    private TeacherProjectVideoRepository teacherProjectVideoRepository;
    @Autowired
    private TeacherTaskWorkOrderRepository teacherTaskWorkOrderRepository;
    @Autowired
    private TeacherWorkOrderFileRepository teacherWorkOrderFileRepository;
    @Autowired
    private TeacherKnowledgePointRepository teacherKnowledgePointRepository;
    @Autowired
    private ProjectGroupTaskRepository projectGroupTaskRepository;
    @Autowired
    private ProjectClassStuRepository projectClassStuRepository;
    @Autowired
    private ProjectPeopleEvaluatingRepository projectPeopleEvaluatingRepository;
    @Autowired
    private TeacherProjectCourseRepository courseRepository;
    @Autowired
    private PostSkillService postSkillService;
    @Autowired
    private ProjectFileRepository fileRepository;
    @Autowired
    private TeacherProjectFileRepository teacherProjectFileRepository;
    @Autowired
    private TeacherKnowledgePointFileRepository teacherKnowledgePointFileRepository;
    @Autowired
    private KnowledgePointFileRepository knowledgePointFileRepository;
    @Autowired
    private ExercisesRepository exercisesRepository;
    @Autowired
    private ExercisesChoiceRepository exercisesChoiceRepository;
    @Autowired
    private ExercisesFileRepository exercisesFileRepository;
    @Autowired
    private TeacherExercisesRepository       teacherExercisesRepository;
    @Autowired
    private TeacherExercisesChoiceRepository teacherExercisesChoiceRepository;
    @Autowired
    private TeacherExercisesFileRepository   teacherExercisesFileRepository;
    @Autowired
    private BusinessRepository businessRepository;
    /**
     * 老师端老师备课
     *
     * @param projectId
     * @param accountDTO
     * @return
     */
    public String publish(String projectId, AccountDTO accountDTO) {
        NewProjectData projectDomain = projectService.findProjectDomain(projectId);
        TeacherProject teacherProject = new TeacherProject();
        BeanUtils.copyProperties(projectDomain, teacherProject);
        teacherProject.setId(UUID.randomUUID().toString());
        teacherProject.setCreatedBy(accountDTO.getId());
        teacherProject.setTemplateId(projectId);
        teacherProject.setCreatedDate(new Date());
        teacherProject.setDeleteFlag(DataValidity.VALID.getState());
        teacherProject.setIsArchive(DataValidity.VALID.getState());
        teacherProject.setIsPublish(DataValidity.VALID.getState());
        List<TeacherProjectPost> projectPosts = new ArrayList<>();
        List<TeacherProjectSkill> skillList = new ArrayList<>();
        List<TeacherProjectSkillPoints> points = new ArrayList<>();
        List<TeacherKnowledgePoint> knowledgePointList = new ArrayList<>();
        List<TeacherProjectModule> teacherProjectModuleList = new ArrayList<>();
        List<TeacherModuleTask> teacherModuleTaskList = new ArrayList<>();
        List<TeacherModuleTaskFile> teacherModuleTaskFileList = new ArrayList<>();
        List<TeacherTaskWorkOrder> teacherTaskWorkOrderList = new ArrayList<>();
        List<TeacherWorkOrderFile> teacherWorkOrderFileList = new ArrayList<>();
        List<TeacherProjectCourse> teacherProjectCourseList = new ArrayList<>();
        List<TeacherProjectFile> teacherProjectFileList = new ArrayList<>();
        List<TeacherKnowledgePointFile> teacherKnowledgePointFileList = new ArrayList<>();
        List<TeacherExercises> teacherExercisesList = new ArrayList<>();
        List<TeacherExercisesFile> teacherExercisesFileList = new ArrayList<>();
        List<TeacherExercisesChoice> teacherExercisesChoiceList = new ArrayList<>();
        for (ProjectCourseDomain projectCourse : projectDomain.getProjectCourseList()) {
            TeacherProjectCourse teacherProjectCourse = new TeacherProjectCourse();
            BeanUtils.copyProperties(projectCourse, teacherProjectCourse);
            teacherProjectCourse.setId(UUID.randomUUID().toString());
            teacherProjectCourse.setProjectId(teacherProject.getId());
            teacherProjectCourseList.add(teacherProjectCourse);
        }
        List<ProjectFile> projectFiles = fileRepository.findByProjectIdOrderByCreatedDateAsc(projectId);
        projectFiles.forEach(item -> {
            TeacherProjectFile teacherProjectFile = new TeacherProjectFile();
            BeanUtils.copyProperties(item, teacherProjectFile);
            teacherProjectFile.setProjectId(teacherProject.getId());
            teacherProjectFile.setId(UUID.randomUUID().toString());
            teacherProjectFileList.add(teacherProjectFile);
        });
        List<TeacherProjectVideo> collect1 = projectDomain.getProjectVideoDomainList().stream().map(video -> {
            TeacherProjectVideo teacherProjectVideo = new TeacherProjectVideo();
            BeanUtils.copyProperties(video, teacherProjectVideo);
            teacherProjectVideo.setId(UUID.randomUUID().toString());
            teacherProjectVideo.setProjectId(teacherProject.getId());
            return teacherProjectVideo;
        }).collect(Collectors.toList());
        for (ProjectPostDomain postDomain : projectDomain.getProjectPostDomainList()) {
            TeacherProjectPost teacherProjectPost = new TeacherProjectPost();
            BeanUtils.copyProperties(postDomain, teacherProjectPost);
            teacherProjectPost.setId(UUID.randomUUID().toString());
            teacherProjectPost.setProjectId(teacherProject.getId());
            projectPosts.add(teacherProjectPost);

            for (ProjectSkillDomain skillDomain : postDomain.getProjectSkillDomainList()) {
                TeacherProjectSkill teacherProjectSkill = new TeacherProjectSkill();
                BeanUtils.copyProperties(skillDomain, teacherProjectSkill);
                teacherProjectSkill.setId(UUID.randomUUID().toString());
                teacherProjectSkill.setPostId(teacherProjectPost.getId());
                skillList.add(teacherProjectSkill);
                for (SkillPointsDomain skillPointsDomain : skillDomain.getPointsList()) {
                    TeacherProjectSkillPoints teacherProjectSkillPoints = new TeacherProjectSkillPoints();
                    BeanUtils.copyProperties(skillPointsDomain, teacherProjectSkillPoints);
                    teacherProjectSkillPoints.setId(UUID.randomUUID().toString());
                    teacherProjectSkillPoints.setSkillId(teacherProjectSkill.getId());
                    points.add(teacherProjectSkillPoints);
                }
            }
        }
        List<ProjectModule> projectModuleList = projectModuleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());

        for (ProjectModule projectModule : projectModuleList) {
            TeacherProjectModule teacherProjectModule = new TeacherProjectModule();
            BeanUtils.copyProperties(projectModule, teacherProjectModule);
            teacherProjectModule.setId(UUID.randomUUID().toString());
            teacherProjectModule.setProjectId(teacherProject.getId());

            List<ModuleTask> moduleTaskList = taskRepository.findByModuleIdAndDeleteFlagOrderByTaskNoAsc(projectModule.getId(), DataValidity.VALID.getState());
            List<String> collect = moduleTaskList.stream().map(ModuleTask::getId).collect(Collectors.toList());
            List<ModuleTaskFile> moduleTaskFileList = taskFileRepository.findByTaskIdInAndDeleteFlag(collect, DataValidity.VALID.getState());
            List<KnowledgePoint> knowledgePoints = knowledgePointRepository.findByModuleTaskIdIn(collect);
            teacherProjectModuleList.add(teacherProjectModule);
            for (ModuleTask moduleTask : moduleTaskList) {

                List<TaskWorkOrder> workOrderList = orderRepository.findByTaskIdAndDeleteFlagOrderByWorkOrderNo(moduleTask.getId(), DataValidity.VALID.getState());
                TeacherModuleTask teacherModuleTask = new TeacherModuleTask();
                BeanUtils.copyProperties(moduleTask, teacherModuleTask);
                teacherModuleTask.setId(UUID.randomUUID().toString());
                teacherModuleTask.setModuleId(teacherProjectModule.getId());
                List<String> orderIds = workOrderList.stream().map(TaskWorkOrder::getId).collect(Collectors.toList());
                List<WorkOrderFile> orderFileList = orderFileRepository.findByWorkOrderIdInAndDeleteFlag(orderIds, DataValidity.VALID.getState());
                //拷贝试题
                List<Exercises> exercisesList = exercisesRepository.findByTaskId(moduleTask.getId());
                exercisesList.forEach(item -> {
                    TeacherExercises teacherExercises = new TeacherExercises();
                    BeanUtils.copyProperties(item,teacherExercises);
                    teacherExercises.setId(UUID.randomUUID().toString());
                    teacherExercises.setTaskId(teacherModuleTask.getId());
                    if (ExerciseType.MULTIPLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.SINGLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.JUDGMENT.getIntValue().equals(item.getQuestionType())) {
                        List<ExercisesChoice> exercisesChoiceList = exercisesChoiceRepository.findByExercisesQuestionIdOrderByOption(item.getId());
                        exercisesChoiceList.forEach(exercisesChoice->{
                            TeacherExercisesChoice teacherExercisesChoice = new TeacherExercisesChoice();
                            BeanUtils.copyProperties(exercisesChoice,teacherExercisesChoice);
                            teacherExercisesChoice.setId(UUID.randomUUID().toString());
                            teacherExercisesChoice.setExercisesQuestionId(teacherExercises.getId());
                            teacherExercisesChoiceList.add(teacherExercisesChoice);
                        });
                    }else {
                        exercisesFileRepository.findByExercisesQuestionId(item.getId()).forEach(file->{
                            TeacherExercisesFile teacherExercisesFile = new TeacherExercisesFile();
                            BeanUtils.copyProperties(file,teacherExercisesFile);
                            teacherExercisesFile.setId(UUID.randomUUID().toString());
                            teacherExercisesFile.setExercisesQuestionId(teacherExercises.getId());
                            teacherExercisesFileList.add(teacherExercisesFile);
                        });
                    }
                    teacherExercisesList.add(teacherExercises);
                });
                for (ModuleTaskFile moduleTaskFile : moduleTaskFileList) {
                    if (moduleTaskFile.getTaskId().equals(moduleTask.getId())) {
                        TeacherModuleTaskFile teacherModuleTaskFile = new TeacherModuleTaskFile();
                        BeanUtils.copyProperties(moduleTaskFile, teacherModuleTaskFile);
                        teacherModuleTaskFile.setId(UUID.randomUUID().toString());
                        teacherModuleTaskFile.setTaskId(teacherModuleTask.getId());
                        teacherModuleTaskFileList.add(teacherModuleTaskFile);
                    }
                }
                for (KnowledgePoint knowledgePoint : knowledgePoints) {
                    if (knowledgePoint.getModuleTaskId().equals(moduleTask.getId())) {
                        TeacherKnowledgePoint teacherKnowledgePoint = new TeacherKnowledgePoint();
                        BeanUtils.copyProperties(knowledgePoint, teacherKnowledgePoint);
                        teacherKnowledgePoint.setId(UUID.randomUUID().toString());
                        teacherKnowledgePoint.setModuleTaskId(teacherModuleTask.getId());
                        knowledgePointList.add(teacherKnowledgePoint);
                        List<KnowledgePointFile> knowledgePointFileList = knowledgePointFileRepository.findByKnowledgePointIdAndDeleteFlag(knowledgePoint.getId(), DataValidity.VALID.getState());
                        knowledgePointFileList.forEach(item -> {
                            TeacherKnowledgePointFile teacherKnowledgePointFile = new TeacherKnowledgePointFile();
                            BeanUtils.copyProperties(item, teacherKnowledgePointFile);
                            teacherKnowledgePointFile.setKnowledgePointId(teacherKnowledgePoint.getId());
                            teacherKnowledgePointFile.setId(UUID.randomUUID().toString());
                            teacherKnowledgePointFileList.add(teacherKnowledgePointFile);
                        });
                    }
                }
                teacherModuleTaskList.add(teacherModuleTask);
                for (TaskWorkOrder taskWorkOrder : workOrderList) {
                    TeacherTaskWorkOrder teacherTaskWorkOrder = new TeacherTaskWorkOrder();
                    BeanUtils.copyProperties(taskWorkOrder, teacherTaskWorkOrder);
                    teacherTaskWorkOrder.setTaskId(teacherModuleTask.getId());
                    teacherTaskWorkOrder.setId(UUID.randomUUID().toString());
                    teacherTaskWorkOrderList.add(teacherTaskWorkOrder);
                    for (WorkOrderFile workOrderFile : orderFileList) {
                        if (workOrderFile.getWorkOrderId().equals(taskWorkOrder.getId())) {
                            TeacherWorkOrderFile teacherWorkOrderFile = new TeacherWorkOrderFile();
                            BeanUtils.copyProperties(workOrderFile, teacherWorkOrderFile);
                            teacherWorkOrderFile.setWorkOrderId(teacherTaskWorkOrder.getId());
                            teacherWorkOrderFile.setId(UUID.randomUUID().toString());
                            teacherWorkOrderFileList.add(teacherWorkOrderFile);
                        }
                    }
                    List<Exercises> exercisesList2 = exercisesRepository.findByTaskId(taskWorkOrder.getId());
                    exercisesList2.forEach(item -> {
                        TeacherExercises teacherExercises = new TeacherExercises();
                        BeanUtils.copyProperties(item,teacherExercises);
                        teacherExercises.setId(UUID.randomUUID().toString());
                        teacherExercises.setTaskId(teacherTaskWorkOrder.getId());
                        if (ExerciseType.MULTIPLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.SINGLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.JUDGMENT.getIntValue().equals(item.getQuestionType())) {
                            List<ExercisesChoice> exercisesChoiceList = exercisesChoiceRepository.findByExercisesQuestionIdOrderByOption(item.getId());
                            exercisesChoiceList.forEach(exercisesChoice->{
                                TeacherExercisesChoice teacherExercisesChoice = new TeacherExercisesChoice();
                                BeanUtils.copyProperties(exercisesChoice,teacherExercisesChoice);
                                teacherExercisesChoice.setId(UUID.randomUUID().toString());
                                teacherExercisesChoice.setExercisesQuestionId(teacherExercises.getId());
                                teacherExercisesChoiceList.add(teacherExercisesChoice);
                            });
                        }else {
                            exercisesFileRepository.findByExercisesQuestionId(item.getId()).forEach(file->{
                                TeacherExercisesFile teacherExercisesFile = new TeacherExercisesFile();
                                BeanUtils.copyProperties(file,teacherExercisesFile);
                                teacherExercisesFile.setId(UUID.randomUUID().toString());
                                teacherExercisesFile.setExercisesQuestionId(teacherExercises.getId());
                                teacherExercisesFileList.add(teacherExercisesFile);
                            });
                        }
                        teacherExercisesList.add(teacherExercises);
                    });
                }
            }
        }
        String images = "";
        if (projectDomain.getProjectScreenshots() != null) {
            for (String s : projectDomain.getProjectScreenshots()) {
                images += s + ",";
            }
            if (images.length() > 1) {
                images = images.substring(0, images.length() - 1);
            }
        }
        String language = "";
        if (projectDomain.getLanguageList() != null) {
            for (Language s : projectDomain.getLanguageList()) {
                language += s.getLanguage() + ",";
            }
            if (language.length() > 1) {
                language = language.substring(0, language.length() - 1);
            }
        }
        teacherProject.setLanguage(language);
        teacherProject.setProjectScreenshots(images);
        teacherProjectFileRepository.save(teacherProjectFileList);
        teacherKnowledgePointFileRepository.save(teacherKnowledgePointFileList);
        teacherProjectRepository.save(teacherProject);
        teacherProjectPostRepository.save(projectPosts);
        teacherProjectSkillRepository.save(skillList);
        teacherProjectSkillPointsRepository.save(points);
        teacherProjectModuleRepository.save(teacherProjectModuleList);
        teacherModuleTaskRepository.save(teacherModuleTaskList);
        teacherModuleTaskFileRepository.save(teacherModuleTaskFileList);
        teacherTaskWorkOrderRepository.save(teacherTaskWorkOrderList);
        teacherWorkOrderFileRepository.save(teacherWorkOrderFileList);
        teacherProjectCourseRepository.save(teacherProjectCourseList);
        teacherKnowledgePointRepository.save(knowledgePointList);
        teacherProjectVideoRepository.save(collect1);
        teacherExercisesRepository.save(teacherExercisesList);
        teacherExercisesChoiceRepository.save(teacherExercisesChoiceList);
        teacherExercisesFileRepository.save(teacherExercisesFileList);
        return teacherProject.getId();
    }

    /**
     * 添加备选项目或练习项目
     *
     * @param
     * @param
     * @param dto
     * @return
     */
    public String addBftProject(saveBftDomain domain, AccountDTO dto) {
        for (String s : domain.getBftProjectId()) {
            String publish = publish(s, dto);
            TeacherProject one = teacherProjectRepository.findOne(publish);
            one.setParentId(domain.getProjectId());
            teacherProjectRepository.save(one);
        }
        return "OK";
    }

    public ProjectTaskProgressVo findTaskProgress(String projectId, String groupId) {
        ProjectTaskProgressVo taskProgressVo = new ProjectTaskProgressVo();
        List<ProjectGroupTask> groupTaskList = projectGroupTaskRepository.findByProjectIdAndGroupIdAndDeleteFlag(projectId, groupId, 0);
        Map<String, List<ProjectGroupTask>> statusMap = groupTaskList.stream().collect(Collectors.groupingBy(b -> b.getTaskStatus()));
        for (Map.Entry<String, List<ProjectGroupTask>> entry : statusMap.entrySet()) {
            String mapKey = entry.getKey();
            List<ProjectGroupTask> mapValue = entry.getValue();
            if (mapKey.equals("init")) {
                taskProgressVo.setNoBegin(mapValue.size());
            } else if (mapKey.equals("running")) {
                taskProgressVo.setBegining(mapValue.size());
            } else if (mapKey.equals("approved")) {
                taskProgressVo.setCompleted(mapValue.size());
//                taskProgressVo.setCompletedRate(groupTaskList.size() / mapValue.size());
            } else if (mapKey.equals("timeout")) {
                taskProgressVo.setDelay(mapValue.size());
//                taskProgressVo.setDelayRate(groupTaskList.size() / mapValue.size());
            }
        }

        taskProgressVo.setAll(groupTaskList.size());
        if (taskProgressVo.getAll() != 0) {
            taskProgressVo.setCompletedRate(taskProgressVo.getCompleted() * 100 / taskProgressVo.getAll());
            taskProgressVo.setDelayRate(taskProgressVo.getDelay() * 100 / taskProgressVo.getAll());
        }
        return taskProgressVo;
    }

    public List<ProjectMemberContributeVo> findMemberContribute(String projectId, String groupId) {
        List<ProjectMemberContributeVo> contributeVoList = new ArrayList<>();
        List<ProjectGroupTask> groupTaskList = projectGroupTaskRepository.findByProjectIdAndGroupIdAndDeleteFlag(projectId, groupId, 0);
        Map<Long, List<ProjectGroupTask>> statusMap = groupTaskList.stream().collect(Collectors.groupingBy(b -> b.getStuId()));
        Integer taskCount = groupTaskList.size();
        for (Map.Entry<Long, List<ProjectGroupTask>> entry : statusMap.entrySet()) {
            ProjectMemberContributeVo memberContributeVo = new ProjectMemberContributeVo();
            Long mapKey = entry.getKey();
            List<ProjectGroupTask> mapValue = entry.getValue();
            User user = userRepository.findOne(mapKey);
            memberContributeVo.setName(user.getName());
            memberContributeVo.setValue(taskCount / mapValue.size());
            contributeVoList.add(memberContributeVo);
        }
        return contributeVoList;
    }

    public Map findReleaseTask(String projectId, String groupId) {
        Map map = new HashMap();
        List<Date> dateList = new ArrayList<>();
        List<Integer> startCountList = new ArrayList<>();
        List<Integer> endCountList = new ArrayList<>();
        List<ProjectGroupTask> groupTaskList = projectGroupTaskRepository.findByProjectIdAndGroupIdAndDeleteFlagOrderByCreatedDateDesc(projectId, groupId, 0);
        Map<String, List<ProjectGroupTask>> statusMap = groupTaskList.stream().collect(Collectors.groupingBy(b -> b.getTaskStatus()));
        Map<Date, List<ProjectGroupTask>> createDateMap = groupTaskList.stream().collect(Collectors.groupingBy(b -> b.getCreatedDate()));
        for (Map.Entry<Date, List<ProjectGroupTask>> entry : createDateMap.entrySet()) {
            dateList.add(entry.getKey());
            List<ProjectGroupTask> mapValue = entry.getValue();
            startCountList.add(mapValue.size());
        }
        for (Map.Entry<String, List<ProjectGroupTask>> entry : statusMap.entrySet()) {
            String mapKey = entry.getKey();
            if (mapKey.equals("approved")) {
                for (ProjectGroupTask data : entry.getValue()) {
                    if (dateList.contains(data.getRealityEndDate())) {
                        continue;
                    }
                    dateList.add(data.getRealityEndDate());
                    endCountList.add(entry.getValue().size());
                }
            }
        }
        List<Date> timeData = dateList.stream().distinct().collect(Collectors.toList());
        timeData.sort(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                return o2.compareTo(o1);
            }
        });
        Set<String> list = new HashSet<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        timeData.forEach(date -> {
            list.add(formatter.format(date));
        });
        map.put("timeList", list);
        map.put("startCountList", startCountList);
        map.put("endCountList", endCountList);
        return map;
    }

    public Map findStuGiveLike(String projectId, String groupId) {
        Map map = new HashMap();
        List<String> nameList = new ArrayList<>();
        List<Integer> giveLikeList = new ArrayList<>();
        List<Integer> remindList = new ArrayList<>();
        List<ProjectClassStu> projectClassStuList = projectClassStuRepository.findByProjectIdAndGroupIdAndDeleteFlag(projectId, groupId, 0);
        projectClassStuList.forEach(item -> {
            User user = userRepository.findOne(item.getStuId());
            nameList.add(user.getName());
            giveLikeList.add(item.getLikeNum());
            remindList.add(item.getUrgeNum());
        });
        ProjectGroup projectGroup = projectGroupRepository.findByIdAndDeleteFlag(groupId, DataValidity.VALID.getState());
        if (!Objects.isNull(projectGroup)) {
            map.put("teamLike", projectGroup.getLikeNum());
            map.put("teamUrgeNum", projectGroup.getUrgeNum());
        } else {
            map.put("teamLike", 0);
            map.put("teamUrgeNum", 0);
        }
        map.put("nameList", nameList);
        map.put("giveLikeList", giveLikeList);
        map.put("remindList", remindList);
        return map;
    }

    public Map findProjectBurnoutFgure(String projectId, String groupId) {
        Map map = new HashMap();
        List<Date> dateList = new ArrayList<>();
        List<Integer> realityCountList = new ArrayList<>();
        List<Integer> planCountList = new ArrayList<>();
        List<ProjectGroupTask> groupTaskList = projectGroupTaskRepository.findByProjectIdAndGroupIdAndTaskStatusNotAndDeleteFlag(projectId, groupId, "approved", 0);
        Map<String, List<ProjectGroupTask>> realityEndDate = groupTaskList.stream().filter(f -> f.getRealityEndDate() != null).collect(Collectors.groupingBy(b -> DateUtil.format(b.getRealityEndDate(), "yyyy-MM-dd")));
        Map<String, List<ProjectGroupTask>> planEndDate = groupTaskList.stream().filter(f -> f.getPlanEndDate() != null).collect(Collectors.groupingBy(b -> DateUtil.format(b.getPlanEndDate(), "yyyy-MM-dd")));
        int taskNum = groupTaskList.size();
        /*for (Map.Entry<Date, List<ProjectGroupTask>> entry : realityEndDate.entrySet()) {
            dateList.add(entry.getKey());
            List<ProjectGroupTask> mapValue = new ArrayList<>();
            mapValue.addAll(entry.getValue());
            realityCountList.add(mapValue.size());
        }

        for (Map.Entry<Date, List<ProjectGroupTask>> entry : planEndDate.entrySet()) {
            dateList.add(entry.getKey());
            List<ProjectGroupTask> mapValue = new ArrayList<>();
            mapValue.addAll(entry.getValue());
            planCountList.add(mapValue.size());
        }*/
        List<Date> datesList = new ArrayList<>();
        datesList.addAll(groupTaskList.stream().filter(f -> f.getRealityEndDate() != null).distinct().map(t -> t.getRealityEndDate()).collect(Collectors.toList()));
        datesList.addAll(groupTaskList.stream().filter(f -> f.getPlanEndDate() != null).distinct().map(t -> t.getPlanEndDate()).collect(Collectors.toList()));
        datesList.sort(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                return o1.compareTo(o2);
            }
        });

        List<String> lists = getBetweenTime(DateUtil.format(datesList.get(0), "yyyy-MM-dd"), DateUtil.format(datesList.get(datesList.size() - 1), "yyyy-MM-dd"));
        lists.forEach(item -> {
            if (realityEndDate.size() > 0) {
                List<ProjectGroupTask> entry = realityEndDate.get(item);
                realityCountList.add(taskNum - (entry == null ? 0 : entry.size()));
            } else {
                realityCountList.add(taskNum - 0);
            }
            if (planEndDate.size() > 0) {
                List<ProjectGroupTask> entry = planEndDate.get(item);
                planCountList.add(taskNum - (entry == null ? 0 : entry.size()));
            } else {
                planCountList.add(taskNum - 0);
            }
        });

       /* List<Date> timeData = dateList.stream().distinct().collect(Collectors.toList());
        timeData.sort(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                return o2.compareTo(o1);
            }
        });*/
        map.put("timeList", lists);
        map.put("realityCountList", realityCountList);
        map.put("planCountList", planCountList);
        return map;
    }

    public static List<String> getBetweenTime(String starttime, String endtime) {
        List<String> betweenTime = new ArrayList<String>();
        try {
            Date sdate = new SimpleDateFormat("yyyy-MM-dd").parse(starttime);
            Date edate = new SimpleDateFormat("yyyy-MM-dd").parse(endtime);

            SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd");

            Calendar sCalendar = Calendar.getInstance();
            sCalendar.setTime(sdate);
            int year = sCalendar.get(Calendar.YEAR);
            int month = sCalendar.get(Calendar.MONTH);
            int day = sCalendar.get(Calendar.DATE);
            sCalendar.set(year, month, day, 0, 0, 0);

            Calendar eCalendar = Calendar.getInstance();
            eCalendar.setTime(edate);
            year = eCalendar.get(Calendar.YEAR);
            month = eCalendar.get(Calendar.MONTH);
            day = eCalendar.get(Calendar.DATE);
            eCalendar.set(year, month, day, 0, 0, 0);

            while (sCalendar.before(eCalendar)) {
                betweenTime.add(outformat.format(sCalendar.getTime()));
                sCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }
            betweenTime.add(outformat.format(eCalendar.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return betweenTime;
    }

    /**
     * copy已备课的课程信息
     *
     * @param id
     * @return
     */
    public String recently(String id) {
        TeacherProject teacherProject = teacherProjectRepository.findOne(id);
        TeacherProject teacherProject1 = new TeacherProject();
        List<ProjectPeopleEvaluating> peopleEvaluatingList = projectPeopleEvaluatingRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());

        BeanUtils.copyProperties(teacherProject, teacherProject1);
        teacherProject1.setId(UUID.randomUUID().toString());
        teacherProject1.setCreatedDate(new Date());
        List<ProjectPeopleEvaluating> collect1 = new ArrayList<>();
        for (ProjectPeopleEvaluating p : peopleEvaluatingList) {
            ProjectPeopleEvaluating projectPeopleEvaluating = new ProjectPeopleEvaluating();
            BeanUtils.copyProperties(p, projectPeopleEvaluating);
            projectPeopleEvaluating.setProjectId(teacherProject1.getId());
            projectPeopleEvaluating.setId(UUID.randomUUID().toString());
            collect1.add(projectPeopleEvaluating);
        }
        List<TeacherProjectCourse> courseList = courseRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        List<TeacherProjectCourse> collect2 = courseList.stream().map(course -> {
            TeacherProjectCourse teacherProjectCourse = new TeacherProjectCourse();
            BeanUtils.copyProperties(course, teacherProjectCourse);
            teacherProjectCourse.setId(UUID.randomUUID().toString());
            teacherProjectCourse.setProjectId(teacherProject1.getId());
            return teacherProjectCourse;
        }).collect(Collectors.toList());

//                peopleEvaluatingList.stream().map(item -> {
//            item.setId(UUID.randomUUID().toString());
//            item.setProjectId(teacherProject1.getId());
//            return item;
//        }).collect(Collectors.toList());
        List<TeacherProjectPost> projectPostList = new ArrayList<>();
        List<TeacherProjectSkill> skillList = new ArrayList<>();
        List<TeacherProjectSkillPoints> pointsList = new ArrayList<>();
        List<TeacherProjectModule> teacherProjectModuleList = new ArrayList<>();
        List<TeacherModuleTask> teacherModuleTaskList = new ArrayList<>();
        List<TeacherModuleTaskFile> teacherModuleTaskFileList = new ArrayList<>();
        List<TeacherTaskWorkOrder> teacherTaskWorkOrderList = new ArrayList<>();
        List<TeacherWorkOrderFile> teacherWorkOrderFileList = new ArrayList<>();
        List<TeacherProjectCourse> teacherProjectCourseList = new ArrayList<>();
        List<TeacherKnowledgePoint> teacherKnowledgePointList = new ArrayList<>();
        List<TeacherProjectFile> teacherProjectFileList = new ArrayList<>();
        List<TeacherKnowledgePointFile> teacherKnowledgePointFileList = new ArrayList<>();
        List<TeacherProjectPost> teacherProjectPosts = teacherProjectPostRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        List<String> postIds = teacherProjectPosts.stream().map(TeacherProjectPost::getId).collect(Collectors.toList());
        List<TeacherProjectSkill> teacherProjectSkills = teacherProjectSkillRepository.findByPostIdInAndDeleteFlag(postIds, DataValidity.VALID.getState());
        List<String> skillIds = teacherProjectSkills.stream().map(TeacherProjectSkill::getId).collect(Collectors.toList());
        List<TeacherProjectSkillPoints> points = teacherProjectSkillPointsRepository.findBySkillIdIn(skillIds);
        List<TeacherExercises> teacherExercisesList = new ArrayList<>();
        List<TeacherExercisesFile> teacherExercisesFileList = new ArrayList<>();
        List<TeacherExercisesChoice> teacherExercisesChoiceList = new ArrayList<>();
        for (TeacherProjectPost teacherProjectPost : teacherProjectPosts) {
            TeacherProjectPost projectPost = new TeacherProjectPost();
            BeanUtils.copyProperties(teacherProjectPost, projectPost);
            projectPost.setId(UUID.randomUUID().toString());
            projectPost.setProjectId(teacherProject1.getId());
            projectPostList.add(projectPost);
            for (TeacherProjectSkill teacherProjectSkill : teacherProjectSkills) {
                if (teacherProjectPost.getId().equals(teacherProjectSkill.getPostId())) {
                    TeacherProjectSkill projectSkill = new TeacherProjectSkill();
                    BeanUtils.copyProperties(teacherProjectSkill, projectSkill);
                    projectSkill.setId(UUID.randomUUID().toString());
                    projectSkill.setPostId(projectPost.getId());
                    skillList.add(projectSkill);
                    for (TeacherProjectSkillPoints teacherProjectSkillPoints : points) {
                        if (teacherProjectSkill.getId().equals(teacherProjectSkillPoints.getSkillId())) {
                            TeacherProjectSkillPoints skillPoints = new TeacherProjectSkillPoints();
                            BeanUtils.copyProperties(teacherProjectSkillPoints, skillPoints);
                            skillPoints.setSkillId(projectSkill.getId());
                            skillPoints.setId(UUID.randomUUID().toString());
                            pointsList.add(skillPoints);
                        }
                    }
                }
            }
        }
        List<TeacherProjectFile> fileList = teacherProjectFileRepository.findByProjectId(id);
        fileList.forEach(item -> {
            TeacherProjectFile teacherProjectFile = new TeacherProjectFile();
            BeanUtils.copyProperties(item, teacherProjectFile);
            teacherProjectFile.setId(UUID.randomUUID().toString());
            teacherProjectFile.setProjectId(teacherProject.getId());
            teacherProjectFileList.add(teacherProjectFile);
        });
        List<TeacherProjectModule> projectModuleList = teacherProjectModuleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(id, DataValidity.VALID.getState());
        for (TeacherProjectModule projectModule : projectModuleList) {
            TeacherProjectModule teacherProjectModule = new TeacherProjectModule();
            BeanUtils.copyProperties(projectModule, teacherProjectModule);
            teacherProjectModule.setId(UUID.randomUUID().toString());
            teacherProjectModule.setProjectId(teacherProject1.getId());
            List<TeacherModuleTask> moduleTaskList = teacherModuleTaskRepository.findByModuleIdAndDeleteFlagOrderByTaskNoAsc(projectModule.getId(), DataValidity.VALID.getState());
            List<String> collect = moduleTaskList.stream().map(TeacherModuleTask::getId).collect(Collectors.toList());
            List<TeacherModuleTaskFile> moduleTaskFileList = teacherModuleTaskFileRepository.findByTaskIdInAndDeleteFlag(collect, DataValidity.VALID.getState());
            List<TeacherKnowledgePoint> teacherKnowledgePoints = teacherKnowledgePointRepository.findByModuleTaskIdIn(collect);

            teacherProjectModuleList.add(teacherProjectModule);
            for (TeacherModuleTask moduleTask : moduleTaskList) {
                List<TeacherTaskWorkOrder> workOrderList = teacherTaskWorkOrderRepository.findByTaskIdAndDeleteFlagOrderByWorkOrderNo(moduleTask.getId(), DataValidity.VALID.getState());
                TeacherModuleTask teacherModuleTask = new TeacherModuleTask();
                BeanUtils.copyProperties(moduleTask, teacherModuleTask);
                teacherModuleTask.setId(UUID.randomUUID().toString());
                teacherModuleTask.setModuleId(teacherProjectModule.getId());

                //拷贝试题
                List<TeacherExercises> exercisesList = teacherExercisesRepository.findByTaskId(moduleTask.getId());
                exercisesList.forEach(item -> {
                    TeacherExercises teacherExercises = new TeacherExercises();
                    BeanUtils.copyProperties(item,teacherExercises);
                    teacherExercises.setId(UUID.randomUUID().toString());
                    teacherExercises.setTaskId(teacherModuleTask.getId());
                    if (ExerciseType.MULTIPLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.SINGLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.JUDGMENT.getIntValue().equals(item.getQuestionType())) {
                        List<TeacherExercisesChoice> exercisesChoiceList = teacherExercisesChoiceRepository.findByExercisesQuestionIdOrderByOption(item.getId());
                        exercisesChoiceList.forEach(exercisesChoice->{
                            TeacherExercisesChoice teacherExercisesChoice = new TeacherExercisesChoice();
                            BeanUtils.copyProperties(exercisesChoice,teacherExercisesChoice);
                            teacherExercisesChoice.setId(UUID.randomUUID().toString());
                            teacherExercisesChoice.setExercisesQuestionId(teacherExercises.getId());
                            teacherExercisesChoiceList.add(teacherExercisesChoice);
                        });
                    }else {
                            teacherExercisesFileRepository.findByExercisesQuestionId(item.getId()).forEach(file->{
                            TeacherExercisesFile teacherExercisesFile = new TeacherExercisesFile();
                            BeanUtils.copyProperties(file,teacherExercisesFile);
                            teacherExercisesFile.setId(UUID.randomUUID().toString());
                            teacherExercisesFile.setExercisesQuestionId(teacherExercises.getId());
                            teacherExercisesFileList.add(teacherExercisesFile);
                        });
                    }
                    teacherExercisesList.add(teacherExercises);
                });
                List<String> orderIds = workOrderList.stream().map(TeacherTaskWorkOrder::getId).collect(Collectors.toList());
                List<TeacherWorkOrderFile> orderFileList = teacherWorkOrderFileRepository.findByWorkOrderIdInAndDeleteFlag(orderIds, DataValidity.VALID.getState());
                for (TeacherModuleTaskFile moduleTaskFile : moduleTaskFileList) {
                    if (moduleTaskFile.getTaskId().equals(moduleTask.getId())) {
                        TeacherModuleTaskFile teacherModuleTaskFile = new TeacherModuleTaskFile();
                        BeanUtils.copyProperties(moduleTaskFile, teacherModuleTaskFile);
                        teacherModuleTaskFile.setId(UUID.randomUUID().toString());
                        teacherModuleTaskFile.setTaskId(teacherModuleTask.getId());
                        teacherModuleTaskFileList.add(teacherModuleTaskFile);
                    }
                }
                for (TeacherKnowledgePoint knowledgePoint : teacherKnowledgePoints) {
                    if (knowledgePoint.getModuleTaskId().equals(moduleTask.getId())) {
                        TeacherKnowledgePoint knowledgePoint1 = new TeacherKnowledgePoint();
                        BeanUtils.copyProperties(knowledgePoint, knowledgePoint1);
                        knowledgePoint1.setId(UUID.randomUUID().toString());
                        knowledgePoint1.setModuleTaskId(teacherModuleTask.getId());
                        teacherKnowledgePointList.add(knowledgePoint1);
                        List<TeacherKnowledgePointFile> knowledgePointFileList = teacherKnowledgePointFileRepository.findByKnowledgePointIdAndDeleteFlag(knowledgePoint.getId(), DataValidity.VALID.getState());
                        knowledgePointFileList.forEach(item -> {
                            TeacherKnowledgePointFile teacherKnowledgePointFile = new TeacherKnowledgePointFile();
                            BeanUtils.copyProperties(item, teacherKnowledgePointFile);
                            teacherKnowledgePointFile.setKnowledgePointId(knowledgePoint.getId());
                            teacherKnowledgePointFile.setId(UUID.randomUUID().toString());
                            teacherKnowledgePointFileList.add(teacherKnowledgePointFile);
                        });
                    }
                }
                teacherModuleTaskList.add(teacherModuleTask);
                for (TeacherTaskWorkOrder taskWorkOrder : workOrderList) {
                    TeacherTaskWorkOrder teacherTaskWorkOrder = new TeacherTaskWorkOrder();
                    BeanUtils.copyProperties(taskWorkOrder, teacherTaskWorkOrder);
                    teacherTaskWorkOrder.setId(UUID.randomUUID().toString());
                    teacherTaskWorkOrder.setTaskId(teacherModuleTask.getId());
                    teacherTaskWorkOrderList.add(teacherTaskWorkOrder);
                    for (TeacherWorkOrderFile workOrderFile : orderFileList) {
                        if (workOrderFile.getWorkOrderId().equals(taskWorkOrder.getId())) {
                            TeacherWorkOrderFile teacherWorkOrderFile = new TeacherWorkOrderFile();
                            BeanUtils.copyProperties(workOrderFile, teacherWorkOrderFile);
                            workOrderFile.setWorkOrderId(teacherTaskWorkOrder.getId());
                            teacherWorkOrderFile.setId(UUID.randomUUID().toString());
                            teacherWorkOrderFileList.add(teacherWorkOrderFile);
                        }
                    }
                    List<TeacherExercises> exercisesList2 = teacherExercisesRepository.findByTaskId(taskWorkOrder.getId());
                    exercisesList2.forEach(item -> {
                        TeacherExercises teacherExercises = new TeacherExercises();
                        BeanUtils.copyProperties(item,teacherExercises);
                        teacherExercises.setId(UUID.randomUUID().toString());
                        teacherExercises.setTaskId(teacherTaskWorkOrder.getId());
                        if (ExerciseType.MULTIPLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.SINGLE.getIntValue().equals(item.getQuestionType()) || ExerciseType.JUDGMENT.getIntValue().equals(item.getQuestionType())) {
                            List<TeacherExercisesChoice> exercisesChoiceList = teacherExercisesChoiceRepository.findByExercisesQuestionIdOrderByOption(item.getId());
                            exercisesChoiceList.forEach(exercisesChoice->{
                                TeacherExercisesChoice teacherExercisesChoice = new TeacherExercisesChoice();
                                BeanUtils.copyProperties(exercisesChoice,teacherExercisesChoice);
                                teacherExercisesChoice.setId(UUID.randomUUID().toString());
                                teacherExercisesChoice.setExercisesQuestionId(teacherExercises.getId());
                                teacherExercisesChoiceList.add(teacherExercisesChoice);
                            });
                        }else {
                            teacherExercisesFileRepository.findByExercisesQuestionId(item.getId()).forEach(file->{
                                TeacherExercisesFile teacherExercisesFile = new TeacherExercisesFile();
                                BeanUtils.copyProperties(file,teacherExercisesFile);
                                teacherExercisesFile.setId(UUID.randomUUID().toString());
                                teacherExercisesFile.setExercisesQuestionId(teacherExercises.getId());
                                teacherExercisesFileList.add(teacherExercisesFile);
                            });
                        }
                        teacherExercisesList.add(teacherExercises);
                    });
                }
            }
        }
        List<TeacherProjectVideo> teacherProjectVideoList = teacherProjectVideoRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());

        List<TeacherProjectVideo> collect3 = teacherProjectVideoList.stream().map(video -> {
            TeacherProjectVideo teacherProjectVideo = new TeacherProjectVideo();
            BeanUtils.copyProperties(video, teacherProjectVideo);
            teacherProjectVideo.setId(UUID.randomUUID().toString());
            teacherProjectVideo.setProjectId(teacherProject1.getId());
            return teacherProjectVideo;
        }).collect(Collectors.toList());
        teacherProjectFileRepository.save(teacherProjectFileList);
        teacherKnowledgePointFileRepository.save(teacherKnowledgePointFileList);
        teacherProjectVideoRepository.save(collect3);
        projectPeopleEvaluatingRepository.save(collect1);
        teacherProjectRepository.save(teacherProject1);
        teacherProjectPostRepository.save(projectPostList);
        teacherProjectSkillRepository.save(skillList);
        teacherProjectSkillPointsRepository.save(pointsList);
        teacherProjectModuleRepository.save(teacherProjectModuleList);
        teacherModuleTaskRepository.save(teacherModuleTaskList);
        teacherModuleTaskFileRepository.save(teacherModuleTaskFileList);
        teacherTaskWorkOrderRepository.save(teacherTaskWorkOrderList);
        teacherWorkOrderFileRepository.save(teacherWorkOrderFileList);
        teacherProjectCourseRepository.save(teacherProjectCourseList);
        teacherKnowledgePointRepository.save(teacherKnowledgePointList);
        courseRepository.save(collect2);
        teacherExercisesRepository.save(teacherExercisesList);
        teacherExercisesChoiceRepository.save(teacherExercisesChoiceList);
        teacherExercisesFileRepository.save(teacherExercisesFileList);
        return teacherProject1.getId();
    }

    /**
     * 老师端老师将备课引入到授课中
     *
     * @param projectId
     * @return
     */
    public String introduceRecently(String projectId) {
        String recently = recently(projectId);
        TeacherProject teacherProject = teacherProjectRepository.findOne(recently);
        teacherProject.setIsPublish(DataValidity.INVALID.getState());
        teacherProjectRepository.save(teacherProject);
        List<TeacherProject> teacherProjectList = teacherProjectRepository.findByParentIdAndDeleteFlag(projectId, DataValidity.VALID.getState());
        for (TeacherProject t : teacherProjectList) {
            String recently1 = recently(t.getId());
            TeacherProject one = teacherProjectRepository.findOne(recently1);
            one.setParentId(recently);
            one.setIsPublish(DataValidity.INVALID.getState());
            teacherProjectRepository.save(one);
        }
        return recently;
    }
    public void aa(String id){
        List<TeacherProjectModule> moduleList = teacherProjectModuleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(id, DataValidity.VALID.getState());
        List<String> collect1 = moduleList.stream().map(TeacherProjectModule::getId).collect(Collectors.toList());
        List<TeacherModuleTask> teacherModuleTaskList = teacherModuleTaskRepository.findByModuleIdInAndDeleteFlagOrderByTaskNoAsc(collect1, DataValidity.VALID.getState());
        List<String> collect = teacherModuleTaskList.stream().map(TeacherModuleTask::getId).collect(Collectors.toList());
        List<TeacherTaskWorkOrder> teacherTaskWorkOrderList = teacherTaskWorkOrderRepository.findByTaskIdInAndDeleteFlagOrderByWorkOrderNo(collect, DataValidity.VALID.getState());
        teacherTaskWorkOrderList.forEach(item->{
            item.setCreatedBy(1927L);
        });
        teacherTaskWorkOrderRepository.save(teacherTaskWorkOrderList);
    }
    /**
     * 老师端查看项目详情信息
     *
     * @param id
     * @return
     */
    public NewProjectData findProjectDomain(String id) {
        TeacherProject project = teacherProjectRepository.findOne(id);
        NewProjectData projectDomain = new NewProjectData();
        BeanUtils.copyProperties(project, projectDomain);
        List<ProjectPostDomain> projectPostDomainList = new ArrayList<>();
        List<TeacherProjectPost> projectPostList = teacherProjectPostRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        for (TeacherProjectPost post : projectPostList) {
            ProjectPostDomain projectPostDomain = new ProjectPostDomain();
            Business business = businessRepository.findByIdAndDeleteFlag(post.getBusinessId(), DataValidity.VALID.getState());
            if (!Objects.isNull(business)){
                projectPostDomain.setBusinessName(business.getBusinessName());
            }
            BeanUtils.copyProperties(post, projectPostDomain);
            String pointNumSql = "SELECT COUNT(*) FROM t_skill_points tsp LEFT JOIN t_project_skill tps ON tsp.skill_id=tsp.id WHERE tps.post_id='" + post.getId() + "'";
            Integer pointNum = pageJdbcUtil.getCountInt(pointNumSql);
            Integer skillNum = teacherProjectSkillRepository.countByPostId(post.getId());
            String numSql = "SELECT count(*) FROM p_skill_points psp LEFT JOIN p_post_skill pps ON psp.skill_id=pps.id WHERE pps.post_id='" + post.getPostId() + "'";
            Integer countInt = pageJdbcUtil.getCountInt(numSql);
            Integer skillNum1 = postSkillService.skillNum(post.getPostId());
            if (skillNum1 + countInt == 0) {
                projectPostDomain.setProportion(0);
            } else {
                projectPostDomain.setProportion((pointNum + skillNum) / (skillNum1 + countInt));
            }
            if (!StringUtils.isEmpty(post.getTransferability())) {
                projectPostDomain.setTransferabilityList(Arrays.asList(post.getTransferability().split(",")));
            }
            List<TeacherProjectSkill> skillList = teacherProjectSkillRepository.findByPostIdAndDeleteFlag(post.getId(), DataValidity.VALID.getState());
            List<ProjectSkillDomain> skillDomainList = new ArrayList<>();
            for (TeacherProjectSkill projectSkill : skillList) {
                ProjectSkillDomain projectSkillDomain = new ProjectSkillDomain();
                BeanUtils.copyProperties(projectSkill, projectSkillDomain);
                List<TeacherProjectSkillPoints> skillPointsList = teacherProjectSkillPointsRepository.findBySkillId(projectSkill.getId());

                List<SkillPointsDomain> pointsDomainList = new ArrayList<>();
                for (TeacherProjectSkillPoints skillPoints : skillPointsList) {
                    SkillPointsDomain pointsDomain = new SkillPointsDomain();
                    BeanUtils.copyProperties(skillPoints, pointsDomain);
                    pointsDomainList.add(pointsDomain);
                }
                projectSkillDomain.setPointsList(pointsDomainList);
                skillDomainList.add(projectSkillDomain);

            }
            projectPostDomain.setProjectSkillDomainList(skillDomainList);
            projectPostDomainList.add(projectPostDomain);
        }

        List<ProjectFileDomain> fileDomainList = new ArrayList<>();
        List<TeacherProjectFile> projectFiles = teacherProjectFileRepository.findByProjectId(id);
        projectFiles.forEach(item -> {
            ProjectFileDomain projectFileDomain = new ProjectFileDomain();
            BeanUtils.copyProperties(item, projectFileDomain);
            fileDomainList.add(projectFileDomain);
        });
        projectDomain.setProjectTeachingPlan(fileDomainList);
        List<ProjectCourseDomain> courseDomainList = new ArrayList<>();
        List<TeacherProjectCourse> courseList = teacherProjectCourseRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        for (TeacherProjectCourse projectCourse : courseList) {
            ProjectCourseDomain courseDomain = new ProjectCourseDomain();
            BeanUtils.copyProperties(projectCourse, courseDomain);
            courseDomainList.add(courseDomain);
        }
        if (project.getLanguage() != null) {
            List<String> stringList = Arrays.asList(project.getLanguage().split(","));
            if (stringList.size()>0) {
                List<Language> languages = languageRepository.findByIdInAndDeleteFlag(stringList, DataValidity.VALID.getState());
                projectDomain.setLanguageList(languages);
            }
        }
        if (project.getProjectScreenshots() != null) {
            projectDomain.setProjectScreenshots(Arrays.asList(project.getProjectScreenshots().split(",")));
        }
        List<ProjectVideoDomain> projectVideoDomainList = new ArrayList<>();
        List<TeacherProjectVideo> videoList = teacherProjectVideoRepository.findByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        for (TeacherProjectVideo projectVideo : videoList) {
            ProjectVideoDomain projectVideoDomain = new ProjectVideoDomain();
            BeanUtils.copyProperties(projectVideo, projectVideoDomain);
            projectVideoDomainList.add(projectVideoDomain);
        }
        projectDomain.setProjectVideoDomainList(projectVideoDomainList);
        projectDomain.setProjectTotalFileNum(countProjectFileNum(id));
        projectDomain.setProjectCourseList(courseDomainList);
//        projectDomain.setProjectVideoDomainList(projectVideoDomainList);
        projectDomain.setProjectPostDomainList(projectPostDomainList);
        return projectDomain;
    }

    RowMapper<String> idRm = new RowMapper<String>() {

        @Override
        public String mapRow(ResultSet rs, int rowNum)
                throws SQLException {
            // TODO Auto-generated method stub
            return rs.getString(1);
        }
    };

    //老师端获取项目下所有的文件数量
    private Integer countProjectFileNum(String id) {
//        String sql = "SELECT COUNT(DISTINCT a.file_id) num FROM (SELECT tpv.id file_id, tpv.project_id project_id FROM c_project_video tpv LEFT JOIN c_project tp ON tpv.project_id=tp.id UNION ALL SELECT tmtf.id file_id,tpm.project_id project_id FROM c_module_task_file tmtf LEFT JOIN c_module_task tmt ON tmtf.task_id=tmt.id LEFT JOIN c_project_module tpm ON tmt.module_id=tpm.id UNION ALL SELECT ttwof.id file_id,tpm.project_id project_id FROM c_task_work_order_file ttwof LEFT JOIN c_task_work_order ttwo ON  ttwof.work_order_id=ttwo.id LEFT JOIN c_module_task tmt ON ttwo.task_id=tmt.id LEFT JOIN c_project_module tpm ON tmt.module_id=tpm.id) a WHERE a.project_id='" + id + "'";
        Integer integer = teacherProjectVideoRepository.countByProjectIdAndDeleteFlag(id, DataValidity.VALID.getState());
        Integer taskFileNum = 0;
        Integer workOrderFileNum = 0;
        List<String> info = pageJdbcUtil.getInfo("SELECT id FROM c_project_module WHERE project_id='" + id + "' AND delete_flag=0", idRm);
        if (info.size() > 0) {
            List<String> taskId = pageJdbcUtil.getInfo("SELECT id FROM c_module_task WHERE module_id in (" + listToString(info) + ") AND delete_flag=0", idRm);
            if (taskId.size() > 0) {
                taskFileNum = teacherModuleTaskFileRepository.countByTaskIdInAndDeleteFlag(taskId, DataValidity.VALID.getState());
                List<String> workOrderId = pageJdbcUtil.getInfo("SELECT id FROM c_task_work_order WHERE task_id in (" + listToString(taskId) + ") AND delete_flag=0", idRm);

                if (workOrderId.size() > 0) {
                    workOrderFileNum = teacherWorkOrderFileRepository.countByWorkOrderIdInAndDeleteFlag(workOrderId, DataValidity.VALID.getState());
                }
            }
        }
        return integer + taskFileNum + workOrderFileNum;
    }

    public String listToString(List<String> list) {
        String s = "";
        for (int i = 0; i < list.size(); i++) {
            s += "'" + list.get(i) + "'";
            if (i != list.size() - 1) {
                s += ",";
            }
        }
        return s;
    }

    /**
     * 老师端编辑项目
     *
     * @param domain
     * @return
     */
    public String save(ProjectDomain domain) {
        TeacherProject project = teacherProjectRepository.findOne(domain.getId());
        Boolean isNew = false;
        List<TeacherProjectPost> projectPostList = new ArrayList<>();
        List<TeacherProjectSkill> projectSkillList = new ArrayList<>();
        List<TeacherProjectVideo> projectVideoList = new ArrayList<>();
        List<TeacherProjectSkillPoints> projectSkillPointsList = new ArrayList<>();
        List<TeacherProjectCourse> courseList = new ArrayList<>();
        BeanUtils.copyProperties(domain, project);
        if (domain.getProjectPostDomainList() != null) {
            for (ProjectPostDomain postDomain : domain.getProjectPostDomainList()) {
                TeacherProjectPost post = new TeacherProjectPost();
                BeanUtils.copyProperties(postDomain, post);
                post.setProjectId(domain.getId());
                post.setId(UUID.randomUUID().toString());
                post.setPostId(postDomain.getId());
                post.setDeleteFlag(DataValidity.VALID.getState());
                if (postDomain.getProjectSkillDomainList() != null) {
                    for (ProjectSkillDomain skillDomain : postDomain.getProjectSkillDomainList()) {
                        TeacherProjectSkill projectSkill = new TeacherProjectSkill();
                        BeanUtils.copyProperties(skillDomain, projectSkill);
                        projectSkill.setPostId(post.getId());
                        projectSkill.setId(UUID.randomUUID().toString());
                        projectSkill.setDeleteFlag(DataValidity.VALID.getState());
                        if (skillDomain.getPointsList() != null) {
                            for (SkillPointsDomain pointsDomain : skillDomain.getPointsList()) {
                                TeacherProjectSkillPoints projectSkillPoints = new TeacherProjectSkillPoints();
                                BeanUtils.copyProperties(pointsDomain, projectSkillPoints);
                                projectSkillPoints.setId(UUID.randomUUID().toString());
                                projectSkillPoints.setSkillId(projectSkill.getId());
                                projectSkillPointsList.add(projectSkillPoints);
                            }
                        }
                        projectSkillList.add(projectSkill);
                    }
                }
                projectPostList.add(post);
            }
        }

        if (domain.getProjectVideoDomainList() != null) {
            for (ProjectVideoDomain videoDomain : domain.getProjectVideoDomainList()) {
                TeacherProjectVideo projectVideo = new TeacherProjectVideo();
                videoDomain.setId(UUID.randomUUID().toString());
                videoDomain.setProjectId(project.getId());
                BeanUtils.copyProperties(videoDomain, projectVideo);
                projectVideo.setDeleteFlag(DataValidity.VALID.getState());
                projectVideoList.add(projectVideo);
            }
        }
        if (domain.getProjectCourseList() != null) {
            for (ProjectCourseDomain courseDomain : domain.getProjectCourseList()) {
                TeacherProjectCourse projectCourse = new TeacherProjectCourse();
                BeanUtils.copyProperties(courseDomain, projectCourse);
                projectCourse.setId(UUID.randomUUID().toString());
                projectCourse.setProjectId(domain.getId());
                courseList.add(projectCourse);
            }
        }
        String images = "";
        if (domain.getProjectScreenshots() != null) {
            for (String s : domain.getProjectScreenshots()) {
                images += s + ",";
            }
            if (images.length() > 1) {
                images = images.substring(0, images.length() - 1);
            }
        }
        String language = domain.getLanguageList();
//        if (domain.getLanguageList() != null) {
//            for (Language s : domain.getLanguageList()) {
//                language += s.getLanguage() + ",";
//            }
//            if (language.length() > 1) {
//                language = language.substring(0, language.length() - 1);
//            }
//        }
        project.setLanguage(language);
        project.setProjectScreenshots(images);

//            deleteProject(domain.getId());
//            courseService.deleteByProjectId(domain.getId());
//            videoService.deleteByProjectId(domain.getId());
        List<TeacherProjectFile> fileList = new ArrayList<>();
        if (domain.getProjectTeachingPlan() != null) {
            domain.getProjectTeachingPlan().forEach(item -> {
                TeacherProjectFile projectFile = new TeacherProjectFile();
                BeanUtils.copyProperties(item, projectFile);
                projectFile.setId(UUID.randomUUID().toString());
                projectFile.setProjectId(domain.getId());
                fileList.add(projectFile);
            });
        }
        deleteProject(project.getId());
        teacherProjectFileRepository.deleteByProjectId(project.getId());
        teacherProjectCourseRepository.deleteByProjectId(project.getId());
        teacherProjectVideoRepository.deleteByProjectId(project.getId());
        teacherProjectFileRepository.save(fileList);
        teacherProjectCourseRepository.save(courseList);
        teacherProjectVideoRepository.save(projectVideoList);
        teacherProjectPostRepository.save(projectPostList);
        teacherProjectSkillRepository.save(projectSkillList);
        teacherProjectSkillPointsRepository.save(projectSkillPointsList);
        teacherProjectRepository.save(project);
        return project.getId();
    }

    public Integer deleteProject(String id) {
        String sql = "DELETE tpp,tps,tsp  FROM c_project tp LEFT JOIN c_project_post tpp ON tp.id=tpp.project_id LEFT JOIN c_project_skill tps ON tpp.id=tps.post_id LEFT JOIN c_skill_points tsp ON tps.id=tsp.skill_id  WHERE tp.id='" + id + "'";
        return pageJdbcUtil.delete(sql);
    }

    /**
     * @param userId
     * @param isPublish
     * @return
     */
    public Map<String, Object> findMyProject(Long userId, Integer isPublish, Integer pageNumber, Integer pageSize, String projectName) {
        Map<String, Object> result = new HashMap<String, Object>();
        projectName = "%" + (projectName == null ? "" : projectName.trim()) + "%";
        String user = "%" + userId + "%";
        Pageable pageable = PageUtil.createRequestAndSortType(pageNumber, pageSize, "desc", "createdDate");
//        Page<TeacherProject> page=teacherProjectRepository.findByCreatedByAndIsPublishAndDeleteFlagAndParentIdIsNullAndProjectNameLikeIgnoreCase(pageable, userId, isPublish, DataValidity.VALID.getState(),projectName);
        Page<TeacherProject> page = teacherProjectRepository.findByCreatedByAndIsPublishAndDeleteFlagAndParentIdIsNullAndProjectNameLikeIgnoreCaseOrOtherTeacherLikeIgnoreCase(pageable, userId, isPublish, DataValidity.VALID.getState(), projectName, user);
        List<TeacherProjectVo> projectDomainList = new ArrayList<>();
        List<TeacherProject> teacherProjectList = page.getContent();
        for (TeacherProject teacherProject : teacherProjectList) {
            TeacherProjectVo projectVo = new TeacherProjectVo();
            BeanUtils.copyProperties(teacherProject, projectVo);
            projectVo.setProjectTotalFileNum(countProjectFileNum(projectVo.getId()));
            List<TeacherProject> teacherProjects = teacherProjectRepository.findByParentIdAndDeleteFlag(projectVo.getId(), DataValidity.VALID.getState());
            List<OtherProjectVo> bftProject = new ArrayList<>();
            List<OtherProjectVo> testProject = new ArrayList<>();
            for (TeacherProject t : teacherProjects) {
                OtherProjectVo otherProjectVo = new OtherProjectVo();
                BeanUtils.copyProperties(t, otherProjectVo);
                if ("备选项目".equals(t.getProjectType())) {
                    bftProject.add(otherProjectVo);
                } else {
                    testProject.add(otherProjectVo);
                }
            }
            projectVo.setBftProject(bftProject);
            projectVo.setTestProject(testProject);
            projectDomainList.add(projectVo);
        }
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNumber(page.getNumber());
        pageDomain.setPageSize(page.getSize());
        pageDomain.setTotalElements(page.getTotalElements());
        pageDomain.setTotalPages(page.getTotalPages());
        result.put(ApiReturnConstants.DATA, projectDomainList);

        result.put(ApiReturnConstants.PAGE, pageDomain);
        return result;
    }

    RowMapper<StuProjectVo> stuVo = new RowMapper<StuProjectVo>() {

        @Override
        public StuProjectVo mapRow(ResultSet rs, int rowNum) throws SQLException {
            // TODO Auto-generated method stub
            StuProjectVo domain = new StuProjectVo();
            domain.setId(rs.getString("id"));
            domain.setProjectName(rs.getString("project_name"));
            domain.setModuleNum(rs.getInt("module_num"));
            domain.setTaskNum(rs.getInt("task_num"));
            domain.setWorkOrderNum(rs.getInt("work_order_num"));
            domain.setClassHour(rs.getInt("class_hour"));
            domain.setBftProject(rs.getInt("bft_num"));
            domain.setTestProject(rs.getInt("test_num"));
            domain.setTeacherName(rs.getString("teacher_name"));
            domain.setTeacherId(rs.getLong("created_by"));
            domain.setCreatedDate(rs.getDate("created_date"));
            domain.setOtherTeacher(rs.getString("other_teacher"));
            domain.setProjectImageUrl(rs.getString("project_image_url"));
            domain.setRole(rs.getString("role"));
            domain.setGroupId(rs.getString("group_id"));
            domain.setParentId(rs.getString("parent_id"));
            domain.setProjectType(rs.getString("project_type"));
            domain.setIsLeaderGrade(rs.getBoolean("is_leader_grade"));
            return domain;
        }
    };

    /**
     * 学生查询我的项目
     *
     * @param domain
     * @param userId
     * @return
     */
    public Map<String, Object> findStuProject(QueryProjectDomain domain, Long userId) {

        String sql = "SELECT cp.id,cp.is_leader_grade,cpc.role, cp.project_name,cp.module_num,cp.task_num,cp.work_order_num,cp.class_hour,cp.project_type,cpc.group_id,cp.parent_id,(SELECT COUNT(*) FROM c_project c WHERE c.parent_id=cp.id AND c.project_type='备选项目' AND c.delete_flag=0) bft_num,(SELECT COUNT(*) FROM c_project c WHERE c.parent_id=cp.id AND c.project_type='练习项目' AND c.delete_flag=0) test_num ,tu.`name` teacher_name, cp.created_date,cp.other_teacher ,cp.created_by,cp.project_image_url FROM c_project_class_stu cpc LEFT JOIN c_project cp ON cpc.project_id=cp.id LEFT JOIN t_user tu ON cp.created_by=tu.id WHERE cpc.stu_id=" + userId + " and cp.project_type = '主讲项目' AND cpc.delete_flag=0 AND cp.delete_flag=0 ";
        String countSql = "SELECT count(*) FROM c_project_class_stu cpc LEFT JOIN c_project cp ON cpc.project_id=cp.id LEFT JOIN t_user tu ON cp.created_by=tu.id WHERE cpc.stu_id=" + userId + " AND cpc.delete_flag=0 and cp.project_type = '主讲项目' AND cp.delete_flag=0 ";
        if (!StringUtils.isEmpty(domain.getProjectName())) {
            sql += " AND cp.project_name LIKE '%" + domain.getProjectName() + "%'";
            countSql += " AND cp.project_name LIKE '%" + domain.getProjectName() + "%'";
        }

        String bSql = "SELECT cp.id,cp.is_leader_grade,cpc.role, cp.project_name,cp.module_num,cp.task_num,cp.work_order_num,cp.class_hour,cp.project_type,cpc.group_id,cp.parent_id,(SELECT COUNT(*) FROM c_project c WHERE c.parent_id=cp.id AND c.project_type='备选项目' AND c.delete_flag=0) bft_num,(SELECT COUNT(*) FROM c_project c WHERE c.parent_id=cp.id AND c.project_type='练习项目' AND c.delete_flag=0) test_num ,tu.`name` teacher_name, cp.created_date,cp.other_teacher ,cp.created_by,cp.project_image_url FROM c_project_class_stu cpc LEFT JOIN c_project cp ON cpc.project_id=cp.id LEFT JOIN t_user tu ON cp.created_by=tu.id WHERE cpc.stu_id=" + userId + " and cp.project_type in('练习项目','备选项目') AND cpc.delete_flag=0 AND cp.delete_flag=0 ";

        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto = new SortDTO();
        dto.setKey("cp.created_date");
        dto.setAsc(false);
        sort.add(dto);
        Map<String, Object> result = pageJdbcUtil.getPageInfor(domain.getPageSize(), domain.getPageNumber(), stuVo, sort, sql, countSql);
        List<StuProjectVo> bResult = pageJdbcUtil.getInfo(bSql, stuVo);
        List<StuProjectVo> stuProjectVoList = (List<StuProjectVo>) result.get(ApiReturnConstants.DATA);
        List<StuProjectVo> stuProjectVos = new ArrayList<>();
        for (StuProjectVo stuProjectVo : stuProjectVoList) {
            if (!StringUtils.isEmpty(stuProjectVo.getOtherTeacher())) {
                List<Long> userIds = new ArrayList<>();
                String[] split = stuProjectVo.getOtherTeacher().split(",");
                for (String s : split) {
                    userIds.add(Long.valueOf(s));
                }
                List<User> userList = userRepository.findAllByIdIn(userIds);
                String other = "";
                for (User user : userList) {
                    other += user.getName() + ",";
                }
                stuProjectVo.setOtherTeacher(other.substring(0, other.length() - 1));
            }
            stuProjectVo.setProjectTotalFileNum(countProjectFileNum(stuProjectVo.getId()));
            stuProjectVo.setClassNum(classRepository.countByProjectIdAndCreatedByAndDeleteFlag(stuProjectVo.getId(), stuProjectVo.getTeacherId(), DataValidity.VALID.getState()));

            stuProjectVos.add(stuProjectVo);
        }

        stuProjectVos.forEach(item -> {
            ProjectClassStu classStu = stuRepository.findByProjectIdAndStuIdAndDeleteFlag(item.getId(), userId, 0);
            bResult.forEach(pj -> {
                if (!StringUtils.isEmpty(classStu.getGroupId())) {
                    if (item.getId().equals(pj.getParentId()) && classStu.getGroupId().equals(pj.getGroupId())) {
                        item.getStuProjectVoList().add(pj);
                    }
                }

            });
        });
        result.put(ApiReturnConstants.DATA, stuProjectVos);
        return result;
    }

    /**
     * 查看项目下的备选项目或练习项目
     *
     * @param projectId
     * @param projectType
     * @return
     */
    public List<TeacherProjectVo> findOtherProject(String projectId, String projectType) {
        List<TeacherProjectVo> projectDomainList = new ArrayList<>();
        List<TeacherProject> teacherProjectList = teacherProjectRepository.findByParentIdAndProjectTypeAndDeleteFlag(projectId, projectType, DataValidity.VALID.getState());
        for (TeacherProject teacherProject : teacherProjectList) {
            TeacherProjectVo projectVo = new TeacherProjectVo();
            BeanUtils.copyProperties(teacherProject, projectVo);
            projectVo.setProjectTotalFileNum(countProjectFileNum(projectVo.getId()));
            projectDomainList.add(projectVo);
        }
        return projectDomainList;
    }

    RowMapper<TeacherInfoVo> teacherInfoVoRowMapper = new RowMapper<TeacherInfoVo>() {

        @Override
        public TeacherInfoVo mapRow(ResultSet rs, int rowNum)
                throws SQLException {
            // TODO Auto-generated method stub
            TeacherInfoVo infoVo = new TeacherInfoVo();
            infoVo.setId(rs.getLong("id"));
            infoVo.setCollegeName(rs.getString("college_name"));
            infoVo.setAvatar(rs.getString("avatar"));
            infoVo.setName(rs.getString("user_name"));
            return infoVo;
        }
    };

    /**
     * 查看其他老师
     *
     * @param projectId
     * @return
     */
    public List<TeacherInfoVo> findAllOtherTeacher(String projectId, String name) {
        TeacherProject teacherProject = teacherProjectRepository.findOne(projectId);
        String ids = 0 + "";
        if (Objects.isNull(teacherProject)) {
            if (teacherProject.getOtherTeacher() != null || !"".equals(teacherProject.getOtherTeacher())) {
                ids = teacherProject.getOtherTeacher();
            }
        }
        String sql = "SELECT tu.id,tc.`NAME` college_name, tu.`name` user_name,tu.AVATAR avatar FROM t_user tu LEFT JOIN t_college tc ON tu.COLLEGE_ID=tc.ID WHERE tu.role='teacher' AND tu.delete_flag=0  AND tu.id NOT in (" + ids + ")";
        if (!StringUtils.isEmpty(name)) {
            sql += " AND tu.`name` like '%" + name + "%'";
        }
        return pageJdbcUtil.getInfo(sql, teacherInfoVoRowMapper);

    }

    /**
     * 查看该项目下的老师
     *
     * @param projectId
     * @return
     */
    public List<TeacherInfoVo> findProjectTeacherList(String projectId) {
        TeacherProject teacherProject = teacherProjectRepository.findOne(projectId);
        List<TeacherInfoVo> info = new ArrayList<>();
        String ids = "";
        if (!Objects.isNull(teacherProject)) {
            ids = teacherProject.getCreatedBy() + "";
            String sql = "SELECT tu.id,tc.`NAME` college_name, tu.`name` user_name,tu.AVATAR avatar FROM t_user tu LEFT JOIN t_college tc ON tu.COLLEGE_ID=tc.ID WHERE tu.role='teacher' AND tu.id in (" + ids + ")";
            info = pageJdbcUtil.getInfo(sql, teacherInfoVoRowMapper);
            ids = "";
            if (teacherProject.getOtherTeacher() != null || !"".equals(teacherProject.getOtherTeacher())) {
                ids += teacherProject.getOtherTeacher();
                String sql1 = "SELECT tu.id,tc.`NAME` college_name, tu.`name` user_name,tu.AVATAR avatar FROM t_user tu LEFT JOIN t_college tc ON tu.COLLEGE_ID=tc.ID WHERE tu.role='teacher' AND tu.id in (" + ids + ")";

                List<TeacherInfoVo> info1 = pageJdbcUtil.getInfo(sql1, teacherInfoVoRowMapper);
                info.addAll(info1);
            }
        }

        return info;

    }

    /**
     * 项目添加其他任课老师
     *
     * @param domain
     * @return
     */
    public Map<String, Object> addProjectTeacher(AddTeacherDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        TeacherProject teacherProject = teacherProjectRepository.findOne(domain.getProjectId());
        if (Objects.isNull(teacherProject)) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "项目不存在");
        } else {
            Set<Long> set = new HashSet<>();
            domain.getUserIds().forEach(e -> {
                set.add(e);
            });
            String ids = "";
            for (Long l : set) {
                ids += l + ",";
            }
            if (ids.length() > 1) {
                ids = ids.substring(0, ids.length() - 1);
            }
            teacherProject.setOtherTeacher(ids);
            teacherProjectRepository.save(teacherProject);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.ERROR, "修改成功");
        }
        return result;
    }

    public Map<String, Object> delete(String id,Long createBy) {
        Map<String, Object> result = new HashMap<String, Object>();
        TeacherProject project = teacherProjectRepository.findOne(id);
        if (!Objects.isNull(project)) {
            if (project.getCreatedBy().equals(createBy)){
                project.setDeleteFlag(DataValidity.INVALID.getState());
                project.setOtherTeacher(null);
            }else {
                project.setOtherTeacher(project.getOtherTeacher().replace(createBy+",","").replace(createBy.toString()+"",""));
            }
            teacherProjectRepository.save(project);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "删除成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未找到项目信息");
        }
        return result;
    }

}
